package com.dsx.myblog.service.impl;

import com.dsx.myblog.dao.UserMapper;
import com.dsx.myblog.entity.User;
import com.dsx.myblog.entity.UserExample;
import com.dsx.myblog.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dsx
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public List<User> list(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UserExample example = new UserExample();
        example.setOrderByClause("id desc");
        return userMapper.selectByExample(example);
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void delete(long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    @Cacheable(value="redidCache", key="'redis_user_' + #id")
    public User get(long id) {
        System.out.println("缓存*******************************************");
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User getUserByName(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
